package com.adam.springsecurity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private UserAuthorityRepository userAuthorityRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private HttpServletRequest request;

    @GetMapping("/user/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
        Object authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("/user/login, context authentication=" + authentication);
        Object authenticationExceptionObject = request.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
        if(authenticationExceptionObject instanceof AuthenticationException) {
            //添加错误信息
            AuthenticationException authenticationException = (AuthenticationException) authenticationExceptionObject;
            modelAndView.addObject("err", authenticationException.getClass().getName() + ":" + authenticationException.getMessage());
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("insertUser")
    public User insertUser() {
        LocalDateTime now = LocalDateTime.now();
        User user = new User();
        user.setUsername("invalid");
        user.setPassword("invalid");
        user.setCreateTime(now);
        userRepository.save(user);
        List<UserAuthority> authorityList = new LinkedList<>();
        user.setUserAuthorities(authorityList);
        UserAuthority userAuthority = new UserAuthority();
        userAuthority.setUserId(user.getId());
        userAuthority.setAuthority(UserAuthority.Authority.ROLE_USER);
        userAuthority.setCreateTime(now);
        authorityList.add(userAuthority);
        userAuthority = new UserAuthority();
        userAuthority.setUserId(user.getId());
        userAuthority.setAuthority(UserAuthority.Authority.ROLE_ADMIN);
        userAuthority.setCreateTime(now);
        authorityList.add(userAuthority);
        userRepository.save(user);
        return user;
    }

    @ResponseBody
    @RequestMapping("insertUserAuthority")
    public UserAuthority insertUserAuthority() {
        LocalDateTime now = LocalDateTime.now();
        UserAuthority userAuthority = new UserAuthority();
        userAuthority.setUserId(-1);
        userAuthority.setAuthority(UserAuthority.Authority.ROLE_USER);
        userAuthority.setCreateTime(now);
        User user = new User();
        user.setUsername("invalid");
        user.setPassword("invalid");
        user.setCreateTime(now);
        userAuthority.setUser(user);
        userAuthorityRepository.save(userAuthority);
        return userAuthority;
    }

    @ResponseBody
    @RequestMapping("queryUser")
    public User queryUser() {
        User user = userRepository.findByUsername("invalid");
        List<UserAuthority> userAuthorityList = user.getUserAuthorities();
        return user;
    }

}